﻿using NoteHelper.Utils;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;

namespace NoteHelper.Sys
{
	public class Sysm002Controller
	{
		private int rowIndex;
		private IBaseView view;
		public Sysm002Controller(IBaseView baseView)
		{
			view = baseView;

			view.Insert += Form_Insert;
			view.Edit += Form_Edit;
			view.Remove += Form_Remove;
			view.Copy += Form_Copy;
			view.Query += Form_Query;
			view.Accept += Form_Accept;
			view.Cancel += Form_Cancel;
			view.Print += Form_Print;
			view.Help += Form_Help;
		}
		private void Form_Insert(object sender, EventArgs e)
		{
			view.RowData = null;
			view.Cmd = FormCommand.Insert;
			view.BeginEdit();
		}
		private void Form_Edit(object sender, EventArgs e)
		{
			BeginEdit(FormCommand.Edit);
		}
		private void Form_Remove(object sender, EventArgs e)
		{
			Form_Cancel(null, null);
			rowIndex = view.GetRowIndex();
			if (rowIndex == -1) return;

			var deleteRow = view.DisplayList.Rows[rowIndex];
			view.DisplayList.Rows.RemoveAt(rowIndex);
			for (int i = rowIndex; i < view.DisplayList.Rows.Count; i++)
			{
				var row = view.DisplayList.Rows[i];
				row["$_rowIndex"] = i;
			}
		}
		private void Form_Copy(object sender, EventArgs e)
		{
			BeginEdit(FormCommand.Insert);
		}
		private void BeginEdit(FormCommand cmd)
		{
			rowIndex = view.GetRowIndex();
			if (rowIndex == -1) return;

			var selectedRow = view.DisplayList.Rows[rowIndex];
			var ht = view.RowData;
			ht.GetValueFormDataRow(selectedRow);
			view.RowData = ht;
			view.Cmd = cmd;
			view.BeginEdit();
		}
		private void Form_Query(object sender, EventArgs e)
		{
			view.RowData = null;
			view.Cmd = FormCommand.Query;
			view.BeginEdit();
		}
		private void Form_Accept(object sender, EventArgs e)
		{
			bool error = false;
			switch (view.Cmd)
			{
				case FormCommand.Display:
					break;
				case FormCommand.Insert:
					//实例化var insertEntity = new Model.ProjectForm();
					// 填充数据
					//view.RowData.FillValueToEntity(insertEntity);
					// 更新数据库
					var insertRow = view.DisplayList.NewRow();
					// 填充数据到insertRow;
					insertRow["$_rowIndex"] = view.DisplayList.Rows.Count;
					view.DisplayList.Rows.Add(insertRow);
					break;
				case FormCommand.Edit:
					var editRow = view.DisplayList.Rows[rowIndex];
					var id = editRow["ObjectID"];
					break;
				case FormCommand.Query:
					Query(view.RowData);
					break;
				default:
					break;
			}
			if (error) return;
			view.RowData = null;
			view.Cmd = FormCommand.Display;
			view.EndEdit();
		}
		private void Query(Hashtable ht)
		{
			// 查询数据库
			var table = new DataTable();
			view.DisplayList = table;
		}
		private void Form_Cancel(object sender, EventArgs e)
		{
			view.RowData = null;
			view.Cmd = FormCommand.Display;
			view.EndEdit();
		}
		private void Form_Print(object sender, EventArgs e)
		{
			throw new NotImplementedException();
		}
		private void Form_Help(object sender, EventArgs e)
		{
			throw new NotImplementedException();
		}
	}
}
